# -*- encoding: utf-8 -*-
"""
@File        : packet_manager.py
@License     : (C)Copyright 2021 LeeRoc (cn) Lipeng
@Modify Time : 2021/8/1 3:37 下午
@Author      : LeeRoc
@Version     : 1.0
@Description :
"""
import sys
import threading

sys.path.append('../')

import os
import shutil
from tqdm import tqdm

from config.Config import *
from tool.current_time import *
from core.data_access_object import packet_table
from core.dc_manage_unit import host_dc_manager

hosts = Get_Cluster_Ip_By_Conf().Get_host()


# test path: '/Volumes/MDisk/CodeLab/Python/Caltech_WebFaces'
class part_packets:
    def __init__(self, raw_packet_path):
        self.packet_manager = packet_table()
        self.__host_manager = host_dc_manager()
        self.raw_packet_path = raw_packet_path
        self.packeted_path = "../data"


    def __get_raw_packet_path_list(self):
        return os.listdir(self.raw_packet_path)

    def __part_move(self, aim_file, path):
        shutil.move(aim_file, path)

    def __zipfile(self, packet_name, path):
        shutil.make_archive(packet_name, 'zip', path)

    def unzip(self, zipfile):
        shutil.unpack_archive(self.packeted_path + '/' + zipfile, extract_dir=self.packeted_path)

    def __list_of_groups(self):
        """
        分组算法， 会有额外的单个损失
        :param list_info: 列表
        :param per_list_len: 每个小列表的长度
        :return:
        """
        list_info = self.__get_raw_packet_path_list()
        per_list_len = int(len(list_info) / len(hosts))
        list_of_group = zip(*(iter(list_info),) * per_list_len)
        end_list = [list(i) for i in list_of_group]  # i is a tuple
        count = len(list_info) % per_list_len
        end_list.append(list_info[-count:]) if count != 0 else end_list

        return end_list

    def packeted_run(self):
        val = self.__list_of_groups()
        for eachs in val:
            os.mkdir(self.raw_packet_path + '/' + str(val.index(eachs)))
            for each in tqdm(eachs):
                self.__part_move(self.raw_packet_path + '/' + each, self.raw_packet_path + '/' + str(val.index(eachs)))
            self.__zipfile(self.packeted_path + '/' + packet_time().packeted_time() + '_' + str(val.index(eachs)),
                           self.raw_packet_path + '/' + str(val.index(eachs)))

    def get_FileSize(self):
        val = os.listdir(self.packeted_path)
        val.remove('.DS_Store')  # 移除产生的中间件文件，防止乱序
        dic = []
        for each in val:
            fsize = os.path.getsize(self.packeted_path + '/' + each)
            fsize = fsize / float(1024 * 1024)
            dic.append(round(fsize, 2))

        return dic

    def distributing_stragegy(self):
        sizes = self.get_FileSize()
        val = os.listdir(self.packeted_path)
        val.remove('.DS_Store')  # 移除产生的中间件文件，防止乱序
        for i in range(0, len(val)):
            self.packet_manager.insert_packet(val[i], hosts[i][0], sizes[i], 'distribute')
            print(val[i], hosts[i][0], sizes[i])
        print(self.packet_manager.select_packet_all())

    def collecting_stragegy(self):
        pass










